/*
 * @Author: xuxu12138 1718324422@qq.com
 * @Date: 2023-07-31 10:52:32
 * @LastEditors: xuxu12138 1718324422@qq.com
 * @LastEditTime: 2024-01-29 09:17:25
 * @FilePath: /things-admin/src/base/api/NewsApi.ts
 * @Description: 这是默认设置,请设置`customMade`, 打开koroFileHeader查看配置 进行设置: https://github.com/OBKoro1/koro1FileHeader/wiki/%E9%85%8D%E7%BD%AE
 */
import { Result } from '@/base/Result';
import { request } from '@/base/Request';
import { Page } from '@/base/Page';
import { News } from '@/base/model/News';
import { TableSort } from '@/base/TableSort';
import { Data} from '@/base/model/Member';

export interface NewsVM {
  actions: string,
  body: string,
  newsType: string,
  pushType: string,
  type: Number,
  targetGroup: string,
  title: string,
  actionData?: string,
  images?: string[],
  pushTime: string,
  targetAccounts?: string[]
}

export function getNewsList(params: {
  pageIndex: number,
  pageSize: number,
  userName: string,
  title: string,
  pushStatus: string,
  createBegin: string,
  createEnd: string,
  companyId?: number[]
}): Promise<Result<Page<News>>> {
  Object.assign(params, TableSort());
  let companyIdString = '';
  if (params.companyId && params.companyId.length > 0) {
    companyIdString = params.companyId.map(x => 'companyId=' + x).join('&');
    delete params.companyId;
  }
  return request.getPage(News, '/api/news?' + companyIdString, { params });
}

export function getNews(id: number): Promise<Result<News>> {
  return request.get(News, `/api/news/${id}`);
}

export function getPush(params: { id: number }): Promise<Result<News>> {
  return request.get(News, '/api/news/push', { params });
}

export function postNewsevocation(id: number): Promise<Result<News>> {
  return request.post(News, `/api/app-push/stop/task/newsId/${id}`);
}

export function putNews(id: number, params: NewsVM): Promise<Result<News>> {
  let formData = new FormData();
  Object.keys(params).forEach(x => {
    formData.append(x, params[x]);
  });
  return request.put(News, `/api/news/${id}`, formData);
}

export function postNews(params: NewsVM): Promise<Result<News>> {
  let formData = new FormData();
  Object.keys(params).forEach(x => {
    formData.append(x, params[x]);
  });
  return request.post(News, '/api/news', formData);
}

export function deleteNews(id: number): Promise<Result<void>> {
  return request.delete(`/api/news/${id}`);
}

//推送次数统计
export function getNewsUsage(params: {
  startTime: any,
  endTime: any
}) {
  return request.getArray(Data, '/api/news/usage/count',{params});
}